package com.zcy.auth.controller;

import com.zcy.auth.service.IMenuService;
import com.zcy.auth.domain.Menu;
import com.zcy.basic.data.ContextData;
import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.AjaxResult;
import com.zcy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.zcy.basic.annotation.PermissionApi;
import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单相关API", description = "菜单的增删改查")
@PermissionApi(name = "菜单管理", description = "菜单相关增删改查")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    @ApiOperation("保存或修改菜单")
    @PermissionApi(name = "保存或修改菜单")
    public AjaxResult saveMenu(@RequestBody Menu menu) {
        try {
            if (menu.getId() != null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 删除菜单对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation("通过Id删除菜单")
    @PermissionApi(name = "通过Id删除菜单")
    public AjaxResult deleteMenuById(@PathVariable("id") Long id) {
        try {
            menuService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 通过Id获取 菜单 实体
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过Id获取菜单")
    @PermissionApi(name = "通过Id获取菜单")
    public AjaxResult getMenu(@PathVariable("id") Long id) {
        try {
            Menu menu =menuService.queryById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 查看所有的菜单实体信息
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有菜单")
    @PermissionApi(name = "查询所有菜单")
    public AjaxResult listAllMenu() {
        try {
            List<Menu> list = menuService.listAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * 分页查询数据
    *
    * @param pageInfo 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation("分页查询菜单")
    @PermissionApi(name = "分页查询菜单")
    public AjaxResult queryMenuPage(@RequestBody PageInfo pageInfo) {
        try {
            PageList<Menu> pageList = menuService.queryPage(pageInfo);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败");
        }
    }

    /**
     * 批量删除
     * @param ids 菜单ID数组
     * @return AjaxResult
     */
    @PatchMapping
    @ApiOperation("批量删除菜单")
    @PermissionApi(name = "批量删除菜单")
    public AjaxResult deleteBatchByMenuIds(@ApiParam("menuID数组") @RequestBody List<Long> ids) {
        try {
            menuService.deleteBatch(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    @GetMapping("/tree")
    @ApiOperation("获取权限菜单树")
    @PermissionApi(name = "获取菜单")
    public AjaxResult queryMenuTreeByEmployeeId(@RequestHeader("token") String token) {
        try {
            List<Menu> menus = menuService.queryMenuTreeByEmployeeId(ContextData.LOGIN_MAP.get(token).getId());
            return AjaxResult.success(menus, "菜单获取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("菜单获取失败");
        }
    }

    @GetMapping("/allTree")
    @ApiOperation("获取全部菜单")
    @PermissionApi(name = "查看全部菜单")
    public AjaxResult queryAllMenuTree() {
        try {
            List<Menu> menus = menuService.getTree();
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("菜单获取失败");
        }
    }

    @GetMapping("/list")
    @ApiOperation("获取可选父级菜单")
    @PermissionApi(name = "获取可选父级菜单")
    public AjaxResult getParentMenus(@RequestParam(name = "id", required = false) String id) {
        try {
            List<Menu> menus = menuService.getParentMenus(id);
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("菜单获取失败");
        }
    }


}
